package com.vdong.shardingorder.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vdong.base.model.BaseResult;
import com.vdong.shardingorder.entity.VdGoods;
import com.vdong.shardingorder.entity.VdOrder;
import com.vdong.shardingorder.entity.VdOrderDetails;
import com.vdong.shardingorder.service.IVdGoodsService;
import com.vdong.shardingorder.service.IVdOrderDetailsService;
import com.vdong.shardingorder.service.IVdOrderService;
import com.vdong.shardingorder.utils.SnowflakeIdWorkerUtils;
import com.vdong.shardingorder.vo.order.req.SaveOrderReqVO;
import com.vdong.shardingorder.vo.order.resp.OrderDetailRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@Api(value = "测试控制器", tags = "测试控制器")
public class TestController {

    @Autowired
    private IVdGoodsService goodsService;
    @Autowired
    private IVdOrderService orderService;
    @Autowired
    private IVdOrderDetailsService detailsService;


    @GetMapping("/init/goods")
    @ApiOperation(value = "初始化商品", notes = "初始化商品")
    public BaseResult<Boolean> saveGoods() {
        ArrayList<VdGoods> list = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            VdGoods goods = new VdGoods();
            goods.setId(SnowflakeIdWorkerUtils.genId());
            goods.setName("商品_" + (i + 1));
            goods.setPrice(RandomUtil.randomInt(10000, 1000000));
            goods.setStock(RandomUtil.randomInt(100, 999));
            goods.setStatus(1);
            goods.setCreateTime(LocalDateTime.now());
            goods.setUpdateTime(LocalDateTime.now());
            list.add(goods);
            if (i % 10000 == 0) {
                goodsService.saveBatch(list);
                list = new ArrayList<>();
            }
        }
        return BaseResult.ok();
    }

    @GetMapping("/init/order")
    @ApiOperation(value = "初始化订单", notes = "初始化订单")
    public BaseResult<Boolean> saveOrder() {
        for (int i = 0; i < 100000; i++) {
            ArrayList<String> longs = new ArrayList<>();
            for (int j = 0; j < 1000; j++) {
                String id = "商品_" + RandomUtil.randomInt(2, 990001);
                longs.add(id);
            }
            LambdaQueryWrapper<VdGoods> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(VdGoods::getName, longs);
            List<VdGoods> goodsList = goodsService.list(wrapper);
            ArrayList<VdOrder> list1 = new ArrayList<>();
            ArrayList<VdOrderDetails> list2 = new ArrayList<>();
            for (VdGoods goods : goodsList) {
                int num = RandomUtil.randomInt(1, 10);
                long orderId = SnowflakeIdWorkerUtils.genId();
                long userId = RandomUtil.randomLong(10000000000L, 10000100000L);
                int totalMoney = goods.getPrice() * num;
                int couponMoney = RandomUtil.randomInt(1, (totalMoney / 2));
                VdOrder order = new VdOrder();
                order.setId(orderId);
                order.setUserId(userId);
                order.setTotalMoney(totalMoney);
                order.setCouponMoney(couponMoney);
                order.setPostageMoney(0);
                order.setActualMoney(totalMoney - couponMoney);
                order.setStatus(RandomUtil.randomInt(1, 5));
                order.setAfterStatus(0);
                VdOrderDetails details = new VdOrderDetails();
                details.setId(SnowflakeIdWorkerUtils.genId());
                details.setUserId(userId);
                details.setOrderId(orderId);
                details.setGoodsId(goods.getId());
                details.setGoodsName(goods.getName());
                details.setPrice(goods.getPrice());
                details.setNum(num);
                details.setMoney(totalMoney);
                details.setImg("https://sf6-ttcdn-tos.pstatp.com/obj/temai/0f642aefe1702a95d75cfcb081c82b6bwww600-600");

                list1.add(order);
                list2.add(details);

            }

            if (list1.size() > 0) {
                orderService.saveBatch(list1);
            }
            if (list2.size() > 0) {
                detailsService.saveBatch(list2);
            }


        }
        return BaseResult.ok();
    }


}
